package com.guet.controller.teacher;

import com.guet.DTO.PaperDTO;
import com.guet.DTO.PaperPageDTO;
import com.guet.DTO.QuestionsDTO;
import com.guet.VO.CorrectAnswerVO;
import com.guet.VO.PaperVO;
import com.guet.VO.QuestionsVO;
import com.guet.mapper.TestMapper;
import com.guet.result.PageResult;
import com.guet.result.Result;
import com.guet.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("teacherTestController")
@RequestMapping("teacher/test")
@Slf4j
@CrossOrigin
public class TestController {
    @Autowired
    private TestService testService;

    /**
     * 保存试卷
     *
     * @return
     */
    @PostMapping("paper/save")
    public Result<Integer> savePaper(@RequestBody PaperDTO paperDTO) {
        log.info("paperDTO:{}", paperDTO);
        Integer id = testService.savePaper(paperDTO);
        return Result.success(id);
    }

    /**
     * 分页查询试卷
     *
     * @param paperPageDTO
     * @return
     */
    @PostMapping("paper")
    public Result<PageResult> getPaperList(@RequestBody PaperPageDTO paperPageDTO) {
        log.info("paperPageDTO:{}", paperPageDTO);
        PageResult result = testService.getPaperList(paperPageDTO);
        return Result.success(result);
    }

    /**
     * 保存题目和选项
     *
     * @param questionsDTOList
     * @param id
     * @return
     */
    @PostMapping("question/save")
    public Result<Object> saveQuestions(@RequestBody List<QuestionsDTO> questionsDTOList, Integer id) {
        log.info("questionsDTO:{}", questionsDTOList);
        testService.saveQuestions(questionsDTOList, id);
        return Result.success();
    }

    /**
     * 根据id查询题目和选项正确答案等信息
     *
     * @param id
     * @return
     */
    @GetMapping("question/{id}")
    public Result<List<QuestionsVO>> getQuestions(@PathVariable Integer id) {
        log.info("question:{}", id);
        List<QuestionsVO> result = testService.queryQuestions(id);
        return Result.success(result);
    }

    /**
     * 根据id查询试卷内容
     *
     * @param id
     * @return
     */
    @GetMapping("paper/{id}")
    public Result<PaperVO> getPaperById(@PathVariable Integer id) {
        log.info("paper:{}", id);
        PaperVO paperVO = testService.getPaperById(id);
        return Result.success(paperVO);
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("paper/{id}")
    public Result<Object> deletePaper(@PathVariable Integer id) {
        log.info("paper:{}", id);
        testService.deletePaperById(id);
        return Result.success();
    }

    /**
     * 发布试卷
     * @param id
     * @return
     */
    @PutMapping("paper/{id}")
    public Result<Object> publishPaper(@PathVariable Integer id) {
        log.info("paper:{}", id);
        testService.publishPaper(id);
        return Result.success();
    }


}
